/*
 * p4620.cpp
 *
 *  Created on: 2013-7-27
 *      Author: zy
 */
#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<algorithm>
using namespace std;
struct node
{
	int id, num, ti, f[11];
} p[31];
int path[31], tp[31];
int use[210];
int ans, n, m, w;
bool cmp(node a, node b)
{
	return a.ti < b.ti;
}
void init()
{
	memset(use, 0, sizeof(use));
	ans = 0;
}
bool check(int x)
{
	int cnt = 0;
	for (int i = 1; i <= p[x].num; i++)
		if (use[p[x].f[i]] == 0)
			cnt++;
	return cnt >= 3;

}
void cut(int x)
{
	for (int i = 1; i <= p[x].num; i++)
		use[p[x].f[i]]++;
}
void uncut(int x)
{
	for (int i = 1; i <= p[x].num; i++)
		use[p[x].f[i]]--;
}

int estimate(int start)
{
	int i, tot = 0, last = start;
	for (i = start + 1; i <= n && p[i].ti - p[last].ti <= w; ++i)
	{
		if (check(i))
		{
			++tot;
			last = i;
		}
	}
	return tot;
}
void dfs(int dep, int len)
{
	if (len > ans)
	{
		ans = len;
		memcpy(path, tp, sizeof(tp));
	}
	for (int i = dep + 1; i <= n && p[i].ti - p[dep].ti <= w; i++)
	{
		if (!check(i))
			continue;
		tp[len + 1] = p[i].id;
		cut(i);
		if (estimate(i) + len + 1 > ans)
			dfs(i, len + 1);
		uncut(i);
	}
}
int main()
{
	int T;
	scanf("%d", &T);
	while (T--)
	{
		init();
		scanf("%d%d%d", &n, &m, &w);
		for (int i = 1; i <= n; i++)
		{
			scanf("%d%d", &p[i].num, &p[i].ti);
			p[i].id = i;
			for (int j = 1; j <= p[i].num; j++)
				scanf("%d", &p[i].f[j]);
		}
		sort(p + 1, p + n + 1, cmp);
		p[0].ti = 2001;
		dfs(0, 0);
		printf("%d\n", ans);
		if (ans == 0)
			continue;
		sort(path + 1, path + ans + 1);
		for (int i = 1; i < ans; i++)
			printf("%d ", path[i]);
		printf("%d\n", path[ans]);
	}
	return 0;
}
